home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / text / hyper / hsc_source.lha / hsc / source / hscprj / writeprj.c < prev   
C/C++ Source or Header  |  1996-10-27  |  8KB  |  313 lines

  1. /*
  2.  * hscprj/writeprj.c
  3.  *
  4.  * project-managment output-routines for hsc
  5.  *
  6.  * Copyright (C) 1995,96  Thomas Aglassinger
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  * updated: 27-Oct-1996
  23.  * created: 10-Sep-1996
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <stdarg.h>
  28. #include <string.h>
  29. #include <errno.h>
  30. #include <time.h>
  31.  
  32. #include "hsclib/ldebug.h"
  33. #include "hscprj/pdebug.h"
  34. #include "hscprj/pdefs.h"
  35.  
  36. #include "ugly/utypes.h"
  37. #include "ugly/dllist.h"
  38. #include "ugly/expstr.h"
  39. #include "ugly/umemory.h"
  40. #include "ugly/infile.h"
  41. #include "ugly/ustring.h"
  42.  
  43. #include "hscprj/document.h"
  44. #include "hscprj/project.h"
  45.  
  46. #define TIMEBUFSIZE 40
  47. static STRARR timebuf[TIMEBUFSIZE];
  48.  
  49. /*
  50.  *-------------------------------------
  51.  * write project file
  52.  *-------------------------------------
  53.  */
  54.  
  55. /*
  56.  * hsc_write_project_file
  57.  *
  58.  * write ids to file
  59.  */
  60. static VOID append_ulong(EXPSTR * dest, ULONG num)
  61. {
  62.     STRARR lenbuf[20];
  63.  
  64.     sprintf(lenbuf, "%lx ", num);
  65.     app_estr(dest, lenbuf);
  66. }
  67.  
  68. static VOID append_string(EXPSTR * dest, STRPTR s)
  69. {
  70.     append_ulong(dest, (ULONG) strlen(s));
  71.     app_estr(dest, s);
  72. }
  73.  
  74. static VOID append_caller(EXPSTR * dest, CALLER * caller)
  75. {
  76. #if 0
  77.     if (caller)
  78.     {
  79.         app_estr(dest, " " ID_CALLER_STR " ");
  80.         append_string(dest, caller->name);
  81.         append_ulong(dest, caller->posx);
  82.         append_ulong(dest, caller->posy);
  83.     }
  84. #endif
  85. }
  86.  
  87. static VOID append_docname(EXPSTR * prjstr, STRPTR docname)
  88. {
  89.     app_estr(prjstr, LINE_DOCUMENT_STR);
  90.     app_estr(prjstr, " ");
  91.     append_string(prjstr, docname);
  92.     app_estr(prjstr, "\n");
  93. }
  94.  
  95. static VOID append_sourcename(EXPSTR * prjstr, STRPTR sourcename)
  96. {
  97.     if (sourcename)
  98.     {
  99.         app_estr(prjstr, LINE_SOURCE_STR);
  100.         app_estr(prjstr, " ");
  101.         append_string(prjstr, sourcename);
  102.         app_estr(prjstr, "\n");
  103.     }
  104. }
  105.  
  106. static VOID append_title(EXPSTR * prjstr, STRPTR title)
  107. {
  108.     if (title)
  109.     {
  110.         app_estr(prjstr, LINE_TITLE_STR);
  111.         app_estr(prjstr, " ");
  112.         append_string(prjstr, title);
  113.         app_estr(prjstr, "\n");
  114.     }
  115. }
  116.  
  117. static VOID append_id(EXPSTR * prjstr, STRPTR id)
  118. {
  119.     app_estr(prjstr, LINE_ID_STR);
  120.     app_estr(prjstr, " ");
  121.     append_string(prjstr, id);
  122.     app_estr(prjstr, "\n");
  123. }
  124.  
  125. static VOID append_include(EXPSTR * prjstr, HSCINC * include)
  126. {
  127.     app_estr(prjstr, LINE_INCLUDE_STR);
  128.     app_estr(prjstr, " ");
  129.     append_string(prjstr, include->name);
  130.     append_caller(prjstr, include->caller);
  131.     app_estr(prjstr, "\n");
  132. #if 0
  133.     DP(fprintf(stderr, DHP "  include `%s'\n", include->name));
  134. #endif
  135. }
  136.  
  137. static VOID append_reference(EXPSTR * prjstr, HSCREF * reference)
  138. {
  139. #if 1
  140.     app_estr(prjstr, LINE_REFERENCE_STR);
  141.     app_estr(prjstr, " ");
  142.     append_string(prjstr, reference->name);
  143.     append_caller(prjstr, reference->caller);
  144.     app_estr(prjstr, "\n");
  145. #else
  146.     DP(fprintf(stderr, DHP "  refers `%s'\n", reference->name));
  147. #endif
  148. }
  149.  
  150. static VOID append_iddef(EXPSTR * prjstr, HSCIDD * iddef)
  151. {
  152.     app_estr(prjstr, LINE_ID_STR);
  153.     app_estr(prjstr, " ");
  154.     append_string(prjstr, iddef->name);
  155.     append_caller(prjstr, iddef->caller);
  156.     app_estr(prjstr, "\n");
  157. }
  158.  
  159. /*
  160.  * append groups of data
  161.  */
  162. static VOID append_header(EXPSTR * prjstr)
  163. {
  164.     time_t now = time(NULL);    /* get current time */
  165.  
  166.     /* create string for current time */
  167.     strftime(timebuf, TIMEBUFSIZE,
  168.              "%d-%b-%Y %H:%M:%S", localtime(&now));
  169.  
  170.     /* append key-sequence, file-format-version and comment */
  171.     app_estr(prjstr,
  172.              FILEID_HSCPRJ "\n" LINE_VERSION_STR " ");
  173.     append_ulong(prjstr, VERSION_HSCPRJ);
  174.     app_estr(prjstr, "\n");
  175.     app_estr(prjstr,
  176.              LINE_REM_STR " Contains all data relevant for project.\n"
  177.              LINE_REM_STR " Maintained by hsc, DO NOT MODIFY!\n");
  178.     app_estr(prjstr,
  179.              LINE_REM_STR " updated: ");
  180.     app_estr(prjstr, timebuf);
  181.     app_estrch(prjstr, '\n');
  182. }
  183.  
  184. /* append included files */
  185. static VOID append_doc_includes(EXPSTR * prjstr, DLLIST * inclist)
  186. {
  187.     DLNODE *nd = dll_first(inclist);
  188.     while (nd)
  189.     {
  190.         append_include(prjstr, (HSCINC *) dln_data(nd));
  191.         nd = dln_next(nd);
  192.     }
  193. }
  194.  
  195. /* append other documents referered */
  196. static VOID append_doc_references(EXPSTR * prjstr, DLLIST * reflist)
  197. {
  198.     DLNODE *nd = dll_first(reflist);
  199.     while (nd)
  200.     {
  201.         append_reference(prjstr, (HSCREF *) dln_data(nd));
  202.         nd = dln_next(nd);
  203.     }
  204. }
  205.  
  206. /* append ids defined within documents */
  207. static VOID append_doc_iddefs(EXPSTR * prjstr, DLLIST * iddefs)
  208. {
  209.     DLNODE *nd = dll_first(iddefs);
  210.     while (nd)
  211.     {
  212.         append_iddef(prjstr, (HSCIDD *) dln_data(nd));
  213.         nd = dln_next(nd);
  214.     }
  215. }
  216.  
  217. /* append all document-related data */
  218. static VOID append_document(EXPSTR * prjstr, HSCDOC * document)
  219. {
  220.     STRPTR docname = document->docname;
  221.  
  222.     append_docname(prjstr, docname);
  223.     append_sourcename(prjstr, document->sourcename);
  224.     append_title(prjstr, estr2str(document->title));
  225.  
  226.     append_doc_iddefs(prjstr, document->iddefs);
  227.     append_doc_includes(prjstr, document->includes);
  228. #if 0
  229.     append_doc_references(prjstr, document->references);
  230. #endif
  231. }
  232.  
  233. /*
  234.  * hsc_write_project_file
  235.  *
  236.  * store all project-data in a string and write it to
  237.  * the file specified with hsc_set_project_filename()
  238.  */
  239. BOOL hsc_project_write_file(HSCPRJ * hp, STRPTR project_fname)
  240. {
  241.     BOOL written = FALSE;
  242.  
  243.     if (hp && !hp->fatal && project_fname)
  244.     {
  245.         EXPSTR *prjstr = init_estr(256);
  246.         DLNODE *nd = NULL;
  247.         FILE *outfile = NULL;
  248.  
  249.         DP(fprintf(stderr, DHP "update project file `%s'\n",
  250.                    project_fname));
  251.  
  252.         /* append header information */
  253.         append_header(prjstr);
  254.  
  255.         /* append current document to project */
  256.         if ((hp->document) && (hp->document->docname))
  257.         {
  258.             hsc_project_add_document(hp);
  259.         }
  260.         else
  261.         {
  262.             DP(fprintf(stderr, DHP "  no new document to add\n"));
  263.         }
  264.  
  265.         /*
  266.          * append all old project info of other files
  267.          */
  268.         nd = dll_first(hp->documents);
  269.         while (nd)
  270.         {
  271.             HSCDOC *document = (HSCDOC *) nd->data;
  272.  
  273.             append_document(prjstr, document);
  274.  
  275.             nd = dln_next(nd);
  276.         }
  277.  
  278.         DP(fprintf(stderr, DHP "project file contains:\n%s", estr2str(prjstr)));
  279.  
  280.         /* write new project file */
  281.         errno = 0;
  282.         outfile = fopen(project_fname, "w");
  283.         if (outfile)
  284.         {
  285.             errno = 0;
  286.             fwrite(estr2str(prjstr), sizeof(char),
  287.                    estrlen(prjstr), outfile);
  288.  
  289.             if (errno)
  290.             {
  291.                 DP(fprintf(stderr, DHP "can't write project file\n"));
  292.                 /* TODO: show message "can't open project file" */
  293.             }
  294.             else
  295.                 written = TRUE;
  296.         }
  297.         else
  298.         {
  299.             DP(fprintf(stderr, DHP "can't open project file for output\n"));
  300.             /* TODO: show message "can't open project file" */
  301.         }
  302.  
  303.         del_estr(prjstr);
  304.     }
  305.     else
  306.     {
  307.         D(fprintf(stderr, DHP "no update project\n"));
  308.     }
  309.  
  310.     return (written);
  311. }
  312.  
  313.